package edu.gmu.projects.fireresponder.comm;


import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.WifiManager;
import android.net.wifi.WifiManager.MulticastLock;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

public class UdpBroadcastService extends Service {
	private MulticastLock multicastLock;

	private final IBinder mBinder = new LocalBinder();
	
	private UDPBroadcast broadcast;

	public IBinder onBind(Intent arg0) {
		return mBinder;
	}

    public class LocalBinder extends Binder {       
    	public UdpBroadcastService getService() {
    		return UdpBroadcastService.this;        
    	}    
    }

	@Override
	public void onCreate() {
		super.onCreate();
		startService();

		Log.i("UdpBroadcastService", "Created UdpBroadcastService service");
	}

	@Override
	public void onDestroy() {
		super.onDestroy();

		Log.i("UdpBroadcastService", "Destroyed UdpBroadcastService service");
	}
	
	private void startService() {
		WifiManager wm = (WifiManager) getSystemService(Context.WIFI_SERVICE);
		multicastLock = wm.createMulticastLock("multiCastLock");
		multicastLock.acquire();
		wm.createWifiLock("wifiLock");

		broadcast = UDPBroadcast.createUDPBroadcast(this, "230.0.0.1", 7557, 255);
		//new EmulatorCommunicator(this);
	}

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // Continue running until it is explicitly stopped
        return START_STICKY;
    }
    
    public MessageCommunicator getMessageCommunicator() {
    	return broadcast;
    }
}
